package com.lz.thymeleaf.controller;



import com.lz.thymeleaf.pojo.User;
import com.lz.thymeleaf.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    //查询后台用户数据，分页和完全查询
    @RequestMapping("list")
    public Map<String,Object> list(@RequestBody Map<String,Object> params){
        List<User> users = userService.getUsers(params);
        int count = userService.findAllUsers(params);
        Map<String,Object> map = new HashMap<>();
        //bootstrap默认格式
        map.put("rows",users);
        map.put("total",count);
        return map;
        }

    //删除用户
    @RequestMapping("remove")
    public Map<String,String> modify(@RequestBody int[] ids){
        userService.delUsers(ids);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        return map;
    }
    //增加用户
    @RequestMapping("add")
    public Map<String,String> addUser(@RequestBody Map<String,String> data){
        userService.addUser(data);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        return map;
    }
    //修改用户
    @RequestMapping("modify")
    public Map<String,String> editUser(@RequestBody User user){
        userService.updateUser(user);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        return map;
    }

}
